package com.duzhuo.springsecurity.config;

import com.duzhuo.springsecurity.handler.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import java.util.ArrayList;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    UserDetailsServiceImp userDetailsServiceImp;

    @Autowired
    TokenManager tokenManager;
    @Autowired
    RedisTemplate redisTemplate;

    //容器中必须要有PasswordEncoder的实例
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.csrf().disable()
                .authorizeRequests().antMatchers("/admin/acl/login", "/helloLogin", "/mylogin", "/user", "/myPhoneLogin", "/login").permitAll()
                .and()
                .authorizeRequests().anyRequest().authenticated()
                .and()
                .exceptionHandling()
                .authenticationEntryPoint(new UnauthEntryPoint())//没有携带token或者token无效
                .accessDeniedHandler(new JWTAccessDeniedHandler())//添加无权限时的处理
                .and()
                /* .addFilterBefore(new PhoneFilter(authenticationManager()),UsernamePasswordAuthenticationFilter.class)*///通过 filter过滤来认证
                .addFilterBefore(new TokenAuthFilter(authenticationManager(), tokenManager, redisTemplate), UsernamePasswordAuthenticationFilter.class)
                .addFilterBefore(new TokenLoginFilter(authenticationManager(), redisTemplate), UsernamePasswordAuthenticationFilter.class)
        ;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(new PhoneCodeAuthenticationProvider())//通过AuthenticationProvider来过滤认证
                .userDetailsService(userDetailsServiceImp).passwordEncoder(passwordEncoder());

    }

    /**
     * 解决 无法直接注入 AuthenticationManager
     *
     * @return
     * @throws Exception
     */
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

}
